package com.sjj.shopping.config;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import service.UserService;

/**
 * className:SecurityConfig
 * Package:com.sjj.shopping.config
 * Description:
 *
 * @Date: 2022/8/7 10:10
 * @Auther: wssky19890629@163.com
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Reference
    UserService userServiceImpl;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //添加自定义的验证器,把步骤5中的service类注入进来，设置加密格式
        auth.userDetailsService(userServiceImpl).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception { //springsecurity通用配置
        http.formLogin()
                .loginProcessingUrl("/tologin")
                .loginPage("/mylogin")
                .successForwardUrl("/welcome");
        http.csrf().disable();  //关闭CSRF验证
        http.authorizeRequests()
                .antMatchers("/user/**").hasAuthority("user")  //访问/user接口需要[user]权限
                .antMatchers("/admin/**").hasAuthority("admin")//访问/admin接口需要[admin]权限
                .antMatchers("/merchant/**").hasAuthority("merchant");//访问/admin接口需要[admin]权限

        http.logout().logoutUrl("/logout").logoutSuccessUrl("/mylogin");



    }
}
